﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace 易发车网.Untils
{
    public static class HaiProxy
    {

        private const string Key = "65f6de4f5492c348f8378730cdef4d5e";


        public static string authHeader(string orderno, string secret, int timestamp, string change)
		{
			//拼装签名字符串
			string planText = string.Format("orderno=%s,secret=%s,timestamp=%d", orderno, secret, timestamp);

			//计算签名
			string sign = GenerateMD5(planText).ToUpper();

			//拼装请求头Proxy-Authorization的值;change 参数: false-换ip ,true-不换ip
			string authHeader = string.Format("sign={0}&orderno={1}&timestamp={2}&change={3}", sign, orderno, timestamp, change);
			return authHeader;
		}


        /// <summary>
        /// MD5字符串加密
        /// </summary>
        /// <param name="txt"></param>
        /// <returns>加密后字符串</returns>
        public static string GenerateMD5(string txt)
        {
            using (MD5 mi = MD5.Create())
            {
                byte[] buffer = Encoding.Default.GetBytes(txt);
                //开始加密
                byte[] newBuffer = mi.ComputeHash(buffer);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < newBuffer.Length; i++)
                {
                    sb.Append(newBuffer[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }
    }
}
